package com.example.a2306_fx.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.example.a2306_fx.R
import com.example.a2306_fx.adapter.CartAdapter
import com.example.a2306_fx.databinding.FragmentCartBinding
import com.example.base.mvvm.BaseFragment


class CartFragment : BaseFragment<FragmentCartBinding,CartViewModel>() {
    lateinit var cartAdapter: CartAdapter

    override fun getLayout(): Int = R.layout.fragment_cart

    override fun getViewModel(): Class<CartViewModel> = CartViewModel::class.java

    override fun initView() {
        cartAdapter = CartAdapter()
        binding.rv.adapter = cartAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)
        //请求数据
        viewModel.cart("true","10","1")
        //点击item上的加  减  全选
        cartAdapter.addChildClickViewIds(R.id.cart_cb,R.id.cart_jian,R.id.cart_jia)
        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.cart_cb->{
                    cartAdapter.data[position].isChecked = !cartAdapter.data[position].isChecked
                }
                R.id.cart_jia->{
                    //页面数据   adapter中数据   后台接口中数据
                    cartAdapter.data[position].cartNum++
                    (cartAdapter.getViewByPosition(position,R.id.cart_num) as TextView).text = cartAdapter.data[position].cartNum.toString()
                    viewModel.cartUpdate(cartAdapter.data[position].id,cartAdapter.data[position].cartNum)
                }
                R.id.cart_jian->{
                    //当数量为1时，不能再减少了
                    if(cartAdapter.data[position].cartNum==1){
                        ToastUtils.showLong("不能再减少了")
                    }else{
                        //页面数据   adapter中数据   后台接口中数据
                        cartAdapter.data[position].cartNum--
                        (cartAdapter.getViewByPosition(position,R.id.cart_num) as TextView).text = cartAdapter.data[position].cartNum.toString()
                        viewModel.cartUpdate(cartAdapter.data[position].id,cartAdapter.data[position].cartNum)
                    }
                }
            }
            //底部金额和选中状态均有可能被影响
            checkBottom()
        }
        //左滑删除
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition//当前下标
                viewModel.cartDelete(mutableListOf(cartAdapter.data[position].id))
                //局部刷新
                cartAdapter.data.removeAt(position)
                cartAdapter.notifyItemRemoved(position)
                //检查底部 //当左滑删除时，上方商品数量也会发生变化
                checkBottom()
            }
        }).attachToRecyclerView(binding.rv)
        //点击管理，显示出删除按钮
        binding.tvManage.setOnClickListener {
            if(binding.tvManage.text.toString()=="管理"){
                binding.tvManage.text = "取消"
                binding.llManage.isVisible = false
                binding.llCancel.isVisible = true
            }else{
                binding.tvManage.text = "管理"
                binding.llManage.isVisible = true
                binding.llCancel.isVisible = false
            }
        }
        //点击删除按钮，进行批量删除
        binding.btnDelete.setOnClickListener {
            //判断有没有选中的
            val filter = cartAdapter.data.filter { it.isChecked }
            if(filter.size>0){
                //获取ids
                var ids = mutableListOf<Int>()
                filter.forEach {
                    ids.add(it.id)
                }
                viewModel.cartDelete(ids)
                //局部刷新
                cartAdapter.data.removeAll(filter)
                cartAdapter.notifyDataSetChanged()
                //检查底部
                checkBottom()
            }else{
                ToastUtils.showLong("请至少选择一种商品")
            }
        }
    }

    private fun checkBottom() {
        //上方商品数量变化
        binding.tvCount.text = "商品数量(${cartAdapter.data.size})"
        //判断是否全选 //筛选出选中的数量和总数量进行比较  总数量不能为0
        val filter = cartAdapter.data.filter { it.isChecked }
        if(cartAdapter.data.size!=0&&filter.size==cartAdapter.data.size){
            binding.cb.isChecked = true
        }else{
            binding.cb.isChecked = false
        }
        binding.cb.text = "合计(${filter.size})"
        //计算金额  一定在这里进行初始化
        var money = 0f
        //选中商品循环累加
        filter.forEach {
            money +=it.cartNum*it.price.toFloat()
        }
        binding.tvMoney.text = "合计:${money}元"


    }

    override fun initData() {
        //观察请求结果
        viewModel.cartSuccess.observe(this){
            if(it.code==200){
                cartAdapter.data.clear()
                cartAdapter.data.addAll(it.data.list)
                cartAdapter.notifyDataSetChanged()
                //更新商品数量
                binding.tvCount.text = "商品数量(${it.data.list.size})"

            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.cartFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.updateSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.updateFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.deleteSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.deleteFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

}